package cn.anc.aonicardv.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

/**
 * Created by yangdai on 2017/12/14.
 */

public class ProgressImageView extends AppCompatImageView {

    private int mProgress = 0;
    private Paint mPaint;
    private Rect rect = new Rect();

    public ProgressImageView(Context context) {
        super(context);
        init();
    }

    public ProgressImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(0x7DE6E6E6);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float p = (float) mProgress / 100;
        int left = (int) (getWidth() * p);
        rect.set(left, 0, getWidth(), getHeight());
        canvas.drawRect(rect, mPaint);
    }
}
